<?php

namespace app\api\controller;

use app\common\model\AdminModel;
use app\common\model\GoodslistModel;
use think\Controller;
use think\Request;

class Goodslist  extends  Base
{
    public function index(Request $request) //// 查询所有的商品信息
    {
        ///获取分页
        $page = $request->param('page');
        $limit = $request->param('limit');
        $key = $request->param('key');

        $db = new GoodslistModel();

        ///判断是否有关键字,如果有关键字，先进行一次筛选
        if(isset($key) && !empty($key)){
//            echo $key;
            $db = $db->where("goods_name", 'like', '%'.$key.'%');     /// 模糊搜索
        }

//        $busid = $this->aid;  ////获取商家商品信息
        $info = $db->where("busid", $this->aid)->page($page, $limit)->select();  //根据商家的id,查找文章
        foreach($info as $kk => $value)
            {
                if($value['people_score_number']==0)$value['score']='没有用户参与打分';
                else $value['score']=number_format($value['total_score']/$value['people_score_number'],2);
            }
        ///获取总数
        $total = $db->where("busid", $this->aid)->count('busid');
        return json(['code'=>1, 'data'=>$info, 'total'=>$total]);
    }

    public function getlist(Request $request)
    {
        $key=$request->param('key');

        $db=new GoodslistModel();

        if(isset($key) && !empty($key)){
            $info = $db->where("busid", $key)->where("status",1)->order(['is_recommended'=>'desc','sellcount'=>'desc'])->select();
            return json(['code'=>1,'data'=>$info]);
        }
    }

    public function getinfo(Request $request)
    {
        $key=$request->param('key');

        $db=new GoodslistModel();

        if(isset($key) && !empty($key)){
            $info = $db->where('id',$key)->select();
            return json(['code'=>1,'data'=>$info]);
        }
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        ////添加和更新操作        根据是否提供busid判断，有id为更新，没有为插入操作
        $data = $request->param();
        $busid = $request->param('busid');

        //return json(["data"=>$data]);
        $db = new GoodslistModel();
        if(isset($data['id']) && !empty($data['id'])){
            /// 更新操作
            $res = $db->save($data, ['id'=>$data['id']]);

            return json(['code'=>1, 'msg'=>'更新成功' ,'data'=>$data]);
        }
        else{
            ////添加操作
            $sell=new AdminModel();
            //echo $data['busid'];
            $info=$sell->where('id',$busid)->find();
            if($info['telephone']==="")
                return json(['code'=>0, 'msg'=>'未填写有效联系方式']);
            else if($info['addr']===""||$info['addr']==="未设置地址")
                return json(['code'=>0, 'msg'=>'未提供有效地址']);
            $res = $db->save($data);
            //echo $db->getLastSql();
        }
        if($res){

            return json(['code'=>1, 'msg'=>'添加成功']);
        }
        else{
            return json(['code'=>0, 'msg'=>'更新失败']);
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(Request $request)
    {
        ////删除该商品
        $id = $request->param('id');
        $db = new GoodslistModel();
        $res = $db->where("id", $id)->delete();
        if($res){
            return json(['code'=>1, 'msg'=>'删除成功']);
        }
        else{
            return json(['code'=>0, 'msg'=>'删除失败']);
        }
    }
}
